package com.ejlchina.tool.util

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import com.ejlchina.tool.constant.DEFAULT_STATUS_BAR_HEIGHT
import com.ejlchina.tool.singleton.GlobalParams.statusBarHeight

fun transparentStatusBar(window: Window) {
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    var systemUiVisibility = window.decorView.systemUiVisibility
    systemUiVisibility =
        systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    window.decorView.systemUiVisibility = systemUiVisibility

    //设置状态栏文字颜色
    setStatusBarTextColor(window, false)
}

fun setStatusBarTextColor(window: Window, light: Boolean) {
    var systemUiVisibility = window.decorView.systemUiVisibility
    systemUiVisibility = if (light) { //白色文字
        systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
    } else { //黑色文字
        systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    }
    window.decorView.systemUiVisibility = systemUiVisibility
}

@SuppressLint("InternalInsetResource")
fun getStatusBarHeight(context: Context) {
    val resId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
    val getStatusBar = context.resources.getDimensionPixelSize(resId)
    statusBarHeight = if (getStatusBar > 0) getStatusBar else DEFAULT_STATUS_BAR_HEIGHT
}


fun fixStatusBarMargin(vararg views: View) {
    views.forEach { view ->
        (view.layoutParams as? ViewGroup.MarginLayoutParams)?.let { lp ->
            lp.topMargin = lp.topMargin + statusBarHeight
            view.requestLayout()
        }
    }
}

fun setNavigationBar(window: Window){
    window.navigationBarColor = Color.WHITE;
}


fun paddingByStatusBar(view: View,isPadding: Boolean = true) {
    view.setPadding(
        view.paddingLeft,
        if (isPadding) statusBarHeight else 0,
        view.paddingRight,
        view.paddingBottom
    )
}